<?php
switch ($_REQUEST['sAction']) {
	case 'job-downloadexcel':
		$oJob = JobHandler::getJob($_REQUEST['iJob']);
		$oExcel = new Excel($oJob);
		header("Content-Type: application/x-msexcel; name=\"{$oExcel->getFilename()}}\"");
		header("Content-Disposition: inline; filename=\"{$oExcel->getFilename()}\"");
		print $oExcel->toString();
		exit();
		break;
	/* To kill? 2011-12-26
	case 'job-hint':
		$iResult = JobHandler::hint($_REQUEST['iJob'], $_SESSION['rid']);
		if ($iResult > 0) {
			header('MiJob-Action: success');
			header('Location: index.php?sView=job-list');
			exit();
		}
		break;
	*/
	case 'job-setowner':
		$iResult = JobHandler::setOwner($_SESSION['iJobID'], $_REQUEST['iUser']);
		if ($iResult == 1) {
			header('Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-setdescription':
		$iResult = JobHandler::setDescription($_REQUEST['iJob'], $_REQUEST['sDescription']);
		if ($iResult == 1) {
			header('Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-customeradd':
		JobHandler::addCustomer($_SESSION['iJobID'], $_REQUEST['iCustomer']);
		break;
	case 'job-customerdelete':
		JobHandler::delCustomer($_SESSION['iJobID'], $_REQUEST['iCustomer']);
		break;
	case 'job-setconfig':
		JobHandler::setConfig($_SESSION['iJobID'], $_REQUEST['sVarName'], $_REQUEST['mVarValue']);
		break;
	case 'job-add':
		$mResult = JobHandler::addJob($_REQUEST['sName']);
		if ($mResult > 0)
		{
			$_SESSION['iJobID'] = $mResult;
			if (@$_REQUEST['iJobgroup']) {
				JobHandler::changeGroup($_SESSION['iJobID'], $_REQUEST['iJobgroup']);
			}
			header('MiJob-Window-Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-change':
		$iChangeResult = 1;
		if (JobHandler::canDo('job', 'write', $_REQUEST['iJob']))
		{
			$iChangeResult = JobHandler::changeJob($_REQUEST['iJob'], $_REQUEST['sName']);
		}
		if ($iChangeResult == 1) {
			$iGroupResult = JobHandler::changeGroup($_REQUEST['iJob'], $_REQUEST['iJobgroup']);
			if ($iChangeResult == 1 && $iGroupResult == 1) {
				header('MiJob-Window-Location: index.php?sView=job-list');
				exit();
			}
		}
		break;
	case 'job-delete':
		$iResult = JobHandler::delJob($_SESSION['iJobID']);
		if ($iResult == 1) {
			header('Location: index.php?sView=job-list');
		}
		break;
	case 'job-urgencyadd':
		$oJobs = JobHandler::getJobs();
		$sUrgencies = $oJobs[$_SESSION['iJobID']]->sUrgencies;
		$sNewUrgencies = array();
		if (count($sUrgencies) == 0) {
			array_push($sNewUrgencies, $_REQUEST['sCaption']);
		}
		else {
			for ($i = 0; $i < count($sUrgencies); $i++) {
				if ($_REQUEST['sInsertWhere']== 'before' && $_REQUEST['iPos'] == $i) {
					array_push($sNewUrgencies, $_REQUEST['sCaption']);
				}
				array_push($sNewUrgencies, $sUrgencies[$i]);
				if ($_REQUEST['sInsertWhere'] == 'after' && $_REQUEST['iPos'] == $i) {
					array_push($sNewUrgencies, $_REQUEST['sCaption']);
				}
			}
		}
		$iResult = JobHandler::changeUrgencies($_SESSION['iJobID'], join('|', $sNewUrgencies));
		if ($iResult == 1) {
			header('Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-urgencydelete':
		$oJobs = JobHandler::getJobs();
		$sUrgencies = $oJobs[$_SESSION['iJobID']]->sUrgencies;
		$sNewUrgencies = array();
		for ($i = 0; $i < count($sUrgencies); $i++) {
			if ($i != $_REQUEST['iUrgency']) {
				array_push($sNewUrgencies, $sUrgencies[$i]);
			}
		}
		$iResult = JobHandler::changeUrgencies($_SESSION['iJobID'], join('|', $sNewUrgencies));
		if ($iResult == 1) {
			header('Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-urgencychange':
		$oJobs = JobHandler::getJobs();
		$sNewUrgencies = $oJobs[$_SESSION['iJobID']]->sUrgencies;
		$sNewUrgencies[$_REQUEST['iUrgency']] = $_REQUEST['sCaption'];
		$iResult = JobHandler::changeUrgencies($_SESSION['iJobID'], join('|', $sNewUrgencies));
		if ($iResult == 1) {
			header('Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-setgroupaccess':
		$iResult = JobHandler::setUsergroupAccess($_SESSION['iJobID'], $_REQUEST['iUsergroup'], $_REQUEST['oAccess']);
		if ($iResult == 1) {
			header('Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-setaccess':
		$iResult = JobHandler::setUserAccess($_SESSION['iJobID'], $_REQUEST['iUser'], $_REQUEST['oAccess']);
		if ($iResult == 1) {
			header('Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-fileadd':
		$iResult = JobHandler::addFile($_SESSION['iJobID'], $_FILES['oFile'], $_REQUEST['sDescription']);
		if ($iResult > 0) {
			header('Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-filechange':
		$iResult = JobHandler::changeFile($_SESSION['iJobID'], $_REQUEST['iFile'], $_REQUEST['sDescription']);
		if ($iResult == 1) {
			header('Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-filedelete':
		$iResult = JobHandler::delFile($_SESSION['iJobID'], $_REQUEST['iFile']);
		if ($iResult == 1) {
			header('Location: index.php?sView=job-view');
			exit();
		}
		break;
	case 'job-filedownload':
		JobHandler::downloadFile($_SESSION['iJobID'], $_REQUEST['sFile']);
		break;
}
?>
